package com.itheima.algorithm.binarysearch;

public class E02Leetcode35 {
    public static int searchInsert1(int[] a, int target) {
        int low = 0, high = a.length - 1;
        while (low <= high) {
            int mid = (low + high) >>> 1;
            long midVal = a[mid];
            if (target < midVal) {
                high = mid - 1;
            } else if (midVal < target) {
                low = mid + 1;
            } else {
                return mid;
            }
        }
        return low;
    }

    public static int searchInsert2(int[] a, int target) {
        int i = 0, j = a.length - 1;
        while (i <= j) {
            int m = (i + j) >>> 1;
            if (target <= a[m]) {
                j = m - 1;
            } else {
                i = m + 1;
            }
        //返回>=target最靠左的值，target将他索引占据，正好插入成功
        }
        return i;
    }


}